<?php
/**
 * @author yuanyitian
 * @date 2023/6/8 15:32
 */

namespace api\admin;

class org
{
    public function add($data){
        $con=con();
        $time=time();
        $parent_id=(int)$data["parent_id"];
        $name=$data["name"];
        $remark=$data["remark"];
        $type=(int)$data["type"];
        $res=mysqli_query($con,"insert into org(parent_id,name,type,create_time,remark) values($parent_id,'$name',$type,$time,'$remark')");
        mysqli_close($con);
        if($res){
            return out_right("新增部门".$name."成功！");
        }else{
            return out_error("新增部门".$name."失败！");
        }
    }
    public function delete($data){
        $con=con();
        $id=(int)$data["id"];
        $res=mysqli_query($con,"update org set status=0 where id=$id");
        mysqli_close($con);
        if($res){
            return out_right("删除部门成功！");
        }else{
            return out_error("删除部门失败！");
        }
    }
    public function update($data){
        $id=(int)$data["id"];
        $type=(int)$data["type"];
        $name=$data["name"];
        $remark=$data["remark"];
        $time=time();

        $con=con();
        $res=mysqli_query($con,"update org set name='$name',type=$type,update_time=$time,remark='$remark' where id=$id");
        mysqli_close($con);
        if($res){
            return out_right("修改部门成功！");
        }else{
            return out_error("修改部门失败！");
        }

    }
    public function select($data){
        $con=con();
        $org=mysqli_query($con,"select * from org where status=1");
        $org=mysqli_fetch_all($org,true);
        mysqli_close($con);
        return out_right($org);
    }

    //查询部门下属账户
//    public function select_user_by_org($data){
//        $con=con();
//        $org_id=(int)$data["org_id"];
//        $res=mysqli_query($con,"select * from account_org_config where org_id=$org_id");
//        $res=mysqli_fetch_all($res,true);
//        if(count($res)!=0){
//            $res=array_column($res,"id");
//            $res=implode(",",$res);
//            $user=mysqli_query($con,"select * from account where id in (".$res.")");
//            $user=mysqli_fetch_all($user, true);
//
//            return out_right($user);
//        }else{
//            return out_right([]);
//        }
//        mysqli_close($con);
//
//    }
}